/**
 * 编辑自建考试题目的答案
 */

/**
 * 提取字符串中的数字
 * @param args
 * @return {*}
 */
function getNumberInString(args){
    if(typeof(args)!='undefined'){
        return args.replace(/[^0-9]/ig, "");
    }
    return -1;
}
$(function () {
    $("#btadddid").click(function () {
        var form = $("#formId");
        var boxType = $('input[name="issingle"]').val();
        var rowDivLength = $(".rowdiv").length;
        var rowdiv = 1;
        if (rowDivLength > 0) {
            rowdiv = parseInt(getNumberInString($(".rowdiv:last").attr("id"))) + 1;
        }

        var textName = "textarea" + rowdiv;
        var answerInput="input"+rowdiv;
        var divId = "rowDiv" + rowdiv;
        var examanswerid="examanswerid"+rowdiv;
        var checkBoxContent='<input name="' + answerInput + '" minNum="0" maxNum="2" value="' + answerInput + '" type="checkbox"> 正确答案 ' ;
        var inputContent=' <input name="right"  type="radio" notNull="true" value="' + answerInput + '"> 正确答案 ';
        if(rowdiv<=1){
            inputContent=' <input name="right"  type="radio" notNull="true" checked="checked" value="' + answerInput + '"> 正确答案 ';
            checkBoxContent=' <input name="' + answerInput + '" minNum="0" maxNum="2" checked="checked" value="' + answerInput + '" type="checkbox"> 正确答案 ' ;
        }

        var content = $('<div class="row rowdiv" id="' + divId + '">'
            + '          <div class="col-xs-2"> '
            + '          </div>'
            + '           <div class="col-xs-4"> '
            + '                  <div class="form-group">'
            + '                      <label for="' + textName + '" class="col-sm-3 control-label">题目答案</label>'
            + '                      <div class="col-sm-9">'
            + '                          <input type="hidden" value="-1" id="' + examanswerid + '" name="' + examanswerid + '"/>'
            + '                          <textarea name="' + textName + '" class="form-control" notNull="true" id="' + textName + '" rows="3"></textarea>'
            + '                      </div> '
            + '                  </div> '
            + '          </div> '
            + '      <div class="col-xs-2">'
            + '          <label class="radio-inline ">   '
            + '             '+inputContent
            + '          </label>   '
            + '       </div> '
            + '       <div class="col-xs-2">  '
            + '              <button type="button" value="' + divId + '" name="' + divId + '" class="btn btn-danger" onclick="deleteRowDiv(' + "'" + divId + "'" + ')">删除</button>   '
            + '      </div>        '
            + '        <div class="col-xs-2">   '
            + '          </div> '
            + '      </div>');


        if (boxType === "0") {
            content = $('<div class="row rowdiv" id="' + divId + '">'
                + '<div class="col-xs-2"> '
                + '   </div>'
                + '     <div class="col-xs-4"> '
                + '          <div class="form-group">'
                + '               <label for="' + textName + '" class="col-sm-3 control-label">题目答案</label>'
                + '               <div class="col-sm-9">'
                + '                          <input type="hidden" value="-1" id="' + examanswerid + '" name="' + examanswerid + '"/>'
                + '                  <textarea name="' + textName + '" class="form-control" notNull="true" id="' + textName + '" rows="3"></textarea>'
                + '              </div> '
                + '          </div> '
                + '      </div> '
                + '      <div class="col-xs-2">'
                + '          <div class="checkbox">   '
                + '                 <label>  '+checkBoxContent
                + '                 </label>   '
                + '          </div> '
                + '        </div>  '
                + '          <div class="col-xs-2">  '
                + '              <button type="button" value="' + divId + '" name="' + divId + '" class="btn btn-danger" onclick="deleteRowDiv(' + "'" + divId + "'" + ')">删除</button>'
                + '          </div>        '
                + '          <div class="col-xs-2">   '
                + '          </div> '
                + '      </div>');
        }

        var submitButton = ' <div class="row" id="rowButton">  '
            + '<div class="col-xs-2">  '
            + ' </div>    '
            + '   <div class="col-xs-4">  '
            + '        <button type="button" id="btnid" class="btn btn-success btn-block" onclick="submitForm()">提交</button>  '
            + '   </div>  '
            + '    <div class="col-xs-2">  '
            + '    </div>   '
            + '</div>';
        if (rowDivLength <= 0) {
            form.append(submitButton);
        }

        if (rowdiv >= 1) {
            $('#rowChoose').css("display", "none");
        }
        content.insertBefore("#rowButton");
    });
});


function deleteRowDiv(obj) {
    var rowDivLength = $(".rowdiv").length;
    var divNum = getNumberInString(obj);
    var inputDelIds=$("#examanswerid"+divNum).val();
    var deleteIds=$("#deleteExamTestIds").val();
    if(inputDelIds!=""||inputDelIds!='undefined'){
        $("#deleteExamTestIds").val(deleteIds+inputDelIds+",");
    }

    if (rowDivLength <= 1) {
        $('#rowButton').remove();
        $('#rowChoose').css("display", "block");
    }
    $('#' + obj).remove();
}

function submitForm() {
    var boxType = $('input[name="issingle"]').val();
    var textareaValues=$("textarea[name^=textarea]");
    var examansweridValues=$("input[name^=examanswerid]");
    var checkedValues=$("input[name^=input]:checked");
    if(boxType=="1"){
        checkedValues=$("input[name^=right]:checked");
    }
    var data="";

    for(var i=0;i<textareaValues.length;i++){
        var textareaNum=getNumberInString(textareaValues[i].name);

        var textareaVal=textareaValues[i].value;
        var examansweridVal=examansweridValues[i].value;
        for(var j=0;j<checkedValues.length;j++){
            var inputNum=getNumberInString(checkedValues[j].name);
            if(boxType=="1"){
                inputNum=getNumberInString(checkedValues[j].value);
            }
            if(textareaNum==inputNum){
                var inputVal=checkedValues[j].value;
                data+=textareaVal+"Γfalse"+inputVal+"Γ"+examansweridVal+"Θ";
            }else{
                data+=textareaVal+"ΓfalseΓ"+examansweridVal+"Θ";
            }
            if(i==textareaValues.length){
                if(textareaNum==inputNum){
                    var inputVal=checkedValues[j].value;
                    data+=textareaVal+"Γfalse"+inputVal+"Γ"+examansweridVal;
                }else{
                    data+=textareaVal+"Γfalse"+"Γ"+examansweridVal;
                }
            }
        }
    }
    $("#data").val(data);
    var zf = new zhForm($("#formId"));
    var isSuccess = zf.verify();
    if (isSuccess) {
        zf.submitForm(deal);
    }
}

/**
 * 提交后的响应函数
 * @param data 服务器返回
 */
function deal(data) {
    alert(data.msg);
    if("编辑成功"==data.msg){
        window.location.href="/sys/test/index.do";
    }
}
